/*
 * ECP菜单
 * */
$(function(){
	$(".layui-nav").find(".parent_menu").remove();
	var headerMenuHtml = '',
    	headerMobileMenuHtml = '',
		leftMenuHtml = '',
		headerMenuCheckDefault = 'layui-this';
	leftMenuCheckDefault = 'style="background: none;"';
	var selected_color=window.localStorage.getItem('layuiAdmin')?JSON.parse(window.localStorage.getItem('layuiAdmin')).theme.color.selected:'#F78400',
		logo_color=window.localStorage.getItem('layuiAdmin')?JSON.parse(window.localStorage.getItem('layuiAdmin')).theme.color.logo:'#F78400';
//	var selected_color=window.localStorage.getItem('layuiAdmin')?window.localStorage.getItem('layuiAdmin');
	$.ajax({
		url:"system/menu/listtreejson",
		data: {'menustate':0},        //id为加载当前角色的权限菜单，为角色id
		type: "post",
		async:false,
		success: function(data){
			$.each(data, function (key, val) {
				headerMenuHtml += '<li class="layui-nav-item parent_menu ' + headerMenuCheckDefault + '" mid="' + val.id + '" > <a href="javascript:;" title="' + val.title + '"><i class="'+val.menuIcon+'"><span>&nbsp;' + val.title + '</span></i></a></li>\n';
				headerMobileMenuHtml += '<dd><a href="javascript:;" id="' + key + 'HeaderId" data-menu="' + key + '"><i class="' + val.icon + '"></i> ' + val.title + '</a></dd>\n';
				var menuList = val.children;
				$.each(menuList, function (index, menu) {
					leftMenuHtml += '<li data-name="' + menu.title + '" class="layui-nav-item" mpid="'+ val.id +'" ' + leftMenuCheckDefault + '>\n';
					if (menu.children != undefined && menu.children != []) {
// 							leftMenuHtml += '<a href="javascript:;" lay-tips="' + menu.title + '" > <i class="layui-icon layui-icon-home"></i> <cite>' + menu.title + '</cite><span class="layui-nav-more"></span></a> ';
						leftMenuHtml += '<a href="javascript:;" lay-tips="' + menu.title + '" > <i class="layui-icon layui-icon-right"></i><i class="'+menu.menuIcon+'"></i> <cite>' + menu.title + '</cite><span class="layui-nav-more"></span></a> ';
						if(menu.children.length > 1){
							leftMenuHtml += '<dl class="layui-nav-child" style="border-top-color:'+selected_color+'">\n';
						}else{
							leftMenuHtml += '<dl class="layui-nav-child" style="width:210px;border-top-color:'+selected_color+'">\n';
						}
						$.each(menu.children, function (childIndex, childMenu) {
							var w100 = '';
							if(childMenu.hrefadd == "#")w100 = 'style="width:165px;"'
							if(childMenu.href != "#" || childMenu.hrefadd != "#"){
								leftMenuHtml += '<dd><span lay-href="' + childMenu.href + '"  lay-text="' + childMenu.title + ' - 查询" class="f_l mcList" '+w100+' title="查询-' + childMenu.title + '">' + childMenu.title + '</span>';
							}else{
								leftMenuHtml += '<dd><span lay-href="' + childMenu.href + '" class="f_l mcList" '+w100+'>' + childMenu.title + '</span>';
							}
// 								if(childMenu.href != "#")leftMenuHtml += '<span class="f_r m_l_5 fz_13" lay-href="' + childMenu.href + '"  lay-text="' + childMenu.title + ' - 查询">查询</span>';
							if(childMenu.hrefadd != "#")leftMenuHtml += '<span class="f_r fz_12 mcAdd" lay-href="' + childMenu.hrefadd + '"  lay-text="' + childMenu.title + ' - 新增" title="新增-' + childMenu.title + '"><i class="fa fa-plus"></i></span>';
							leftMenuHtml += '</dd>';
						})
					} else {
						leftMenuHtml += '<a lay-href="' + menu.href + '" ><i class="' + menu.icon + '"></i><span class="layui-left-nav"> ' + menu.title + '</span></a>\n';
					}
					leftMenuHtml += '</dl>\n<hr style="margin: 0;padding:0;transform: scaleY(.5);background-color: rgba(255,255,255,0.25)"/>';
					leftMenuHtml += '</li>\n';
				});
				headerMenuCheckDefault = '';
				leftMenuCheckDefault = 'style="display:none;background: none;"';
			});
			$('.layui-header-menu').find(".layadmin-flexible").after(headerMenuHtml); //电脑
//                 $('.layui-header-mini-menu').html(headerMobileMenuHtml); //手机
			$('.layui-left-menu-tree').html(leftMenuHtml);
// 	                 $('.layui-left-menu-tree li').last().after(leftMenuHtml);
		}
	});

	$('.layui-header-menu').find(".parent_menu").click(function(){
		var mid = $(this).attr('mid');
		$('.layui-left-menu-tree li').css('display','none');
		$('.layui-left-menu-tree li').each(function(index){
			if($(this).attr("mpid") == mid)$(this).css('display','')
		});
	})
	$(".mcAdd").hover(function(){
		$(this).find('i').addClass('fa-plus-this');
	},function(){
		$(this).find('i').removeClass('fa-plus-this');
	})
//	屏蔽IE浏览器的回退键
	if (window.history && window.history.pushState) {
		　　$(window).on('popstate', function () {
		　　window.history.pushState('forward', null, null);
		　　window.history.forward(1);
	　　});
	}
	window.history.pushState('forward', null, null); //在IE中必须得有这两行
	window.history.forward(1);
//	监听tab页——颜色渲染
	layui.use('element', function(){
		element = layui.element;
		element.on('tab()', function(data){
//			console.log(this); //当前Tab标题所在的原始DOM元素
//			console.log(data.index); //得到当前Tab的所在下标
//			console.log(data.elem); //得到当前的Tab大容器
			$('.layadmin-pagetabs .layui-tab-title li').css('background','none');//所有tab页变为无背景颜色
			$('.layadmin-pagetabs .layui-tab-title li').css('color','#000');
			$('.layadmin-pagetabs .layui-tab-title li.layui-this').css('background',logo_color);//选中tab页背景颜色变为主题颜色
			$('.layadmin-pagetabs .layui-tab-title li.layui-this').css('color','#fff');
			$('.layadmin-pagetabs .layui-tab-title li').hover(function(){
				$(this).css('background',logo_color);
				$(this).css('color','#fff');
			},function(){
				$(this).css('background','none');
				$(this).css('color','#000');
				$('.layadmin-pagetabs .layui-tab-title li.layui-this').css('background',logo_color);//再次渲染，防止鼠标放在当前tab页
				$('.layadmin-pagetabs .layui-tab-title li.layui-this').css('color','#fff');
			})
			$('.layadmin-pagetabs .layui-tab-title li').click(function(){
				$('.layadmin-pagetabs .layui-tab-title li').css('background','none');
				$('.layadmin-pagetabs .layui-tab-title li').css('color','#000');
				$('.layadmin-pagetabs .layui-tab-title li.layui-this').css('background',logo_color);
				$('.layadmin-pagetabs .layui-tab-title li.layui-this').css('color','#fff');
			})
		});
	});
})
	